package main

import (
	_ "errors"
	"fmt"
	_ "time"

	"github.com/garyburd/redigo/redis"
)

// go 语言的数组默认是值传递，拷贝值
func test(arr [3]int) {
	arr[0] = 9
	arr[1] = 9
	arr[2] = 9
}

// 想要实现起来说的效果必须使用指针
// 长度是数组的一部分，这里arr *[3]int 指定了3 那么传递进来的数组只允许有3个元素
// 给成 arr *[4]int 但是传递进来的是 arr [3]int
// 就会报错 ： cannot use &numArr03 (type *[3]int) as type *[4]int in argument to test2
func test2(arr *[3]int) {
	(*arr)[0] = 9
	(*arr)[1] = 9
	(*arr)[2] = 9
}

func main() {
	//1 数组的四种初始化方式
	var numArr01 [3]int = [3]int{1, 2, 3}
	fmt.Println("numArr01=", numArr01)

	var numArr02 = [3]int{5, 6, 7}
	fmt.Println("numArr02=", numArr02)
	//这里的 [...] 是规定的写法
	var numArr03 = [...]int{8, 9, 10}
	fmt.Println("numArr03=", numArr03)

	var numArr04 = [...]int{1: 800, 0: 900, 2: 999}
	fmt.Println("numArr04=", numArr04)

	//类型推导
	strArr05 := [...]string{1: "tom", 0: "jack", 2: "mary"}
	fmt.Println("strArr05=", strArr05)

	strArr06 := []string{"a", "b", "c"}

	fmt.Printf("len(strArr06) = %v, cap(strArr06) = %v \n", len(strArr06), cap(strArr06))

	//2 数组的遍历方式
	//2.1普通遍历
	for i := 0; i < len(numArr01); i++ {
		fmt.Printf("numArr01[%d] = %v\n", i, numArr01[i])
	}

	//2.2 for index,value := range arr {}
	for index, value := range numArr02 {
		fmt.Printf("numArr02[%d] = %v\n", index, value)
	}

	//3 数组是值传递
	// 拷贝值传递
	// numArr03[3] = 10// 定义的时候初始化了长度不能够再赋值
	test(numArr03)
	fmt.Println("test(numArr03) : ", numArr03) //test(numArr03) :  [8 9 10]

	// 将数组的地址传递进去
	test2(&numArr03)
	fmt.Println("test2(&numArr03) : ", numArr03) //test2(numArr03) :  [9 9 9]

	// 二维数组
	var numArr05 [3][3]int = [3][3]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
	fmt.Println("numArr05 = ", numArr05)

	// 4.  ... 操作符只适用于[]interface{} 切片，会把切片内每个元素使用空格连接
	arr := []string{"1", "2", "3"}
	fmt.Println(arr)//[1 2 3]
	fmt.Println(redis.Args{}.AddFlat(arr)...)//1 2 3
	srrInterface := []interface{}{"1", "2", "3"}
	fmt.Println(srrInterface)//[1 2 3]
	fmt.Println(srrInterface...)//1 2 3
}
